<?php
include("../includes/DBFunctions.php");
include("../includes/getCredentials.php");

function yearlyReportFunctor($rid) {
	$db = new DBFunctions;
	$gc = new GetCredentials;
	$uid = $gc->getUserID();
	$db->connectDB();
	$query = "call getLog(".$uid.",".$rid.",@a);";
	$result = $db->query($query);
	if( !$result ) {
		die("alert('Failed stored procedure call (getLog)');");
	}
	if (mysqli_num_rows($result) < 1) {
		$db->closeDB();
		$toPrint = "<div><h3>n/a</h3></div>";
	} else {
		$toPrint = "";
		while( $line = mysqli_fetch_array( $result, MYSQL_ASSOC ) ) {
			$onclick = "setTitle(\\\"<h2>Animal Report</h2>\\\");var s=new Array(\\\"sid=".$line['Species_ID']."\\\");call(\\\"functors/animalReportFunctor.php\\\",s,1,0);";
			$toPrint .= "<div><h3><a href='#' onclick='".$onclick."'>".$line['CommonName']."</a></h3><ul>";
			$toPrint .= "<li>".$line['Old Population']." &rarr; ".$line['New Population']."</li>";
			$toPrint .= "</ul></div>";
		}
		$db->closeDB();
	}
	
	echo 'displayReport("'.$toPrint.'");';
}

if( !isset($_POST['rid']) || $_POST['rid'] == '') {
	die("alert('No region was selected. Please try again.');");
}
$rid = $_POST['rid'];
yearlyReportFunctor($rid);
?>